package com.hz.pay.center.customer;

import com.hz.pay.center.config.MqConfig;
import com.hz.pay.center.model.NoticeResultInfo;
import com.hz.pay.center.service.INoticeResultService;
import com.hz.pay.center.service.INoticeRuleService;
import com.pay.common.resp.PayNotifyData;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Date;

/**
 * 支付异步回调消费者
 */
@Component
public class PayNotifyCustomer {


    private Logger logger = LoggerFactory.getLogger(PayNotifyCustomer.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private INoticeResultService resultService;

    @Autowired
    private INoticeRuleService noticeRuleService;


    /**
     * 消费队列中的消息发送通知到业务系统
     * @param channel
     * @param message
     * @param payNotifyData
     */
    @RabbitListener(queues = MqConfig.TTL_PAY_NOTIFY_QUEUE)
    public void  process(Channel channel, Message message, PayNotifyData payNotifyData){

        logger.info("消费者消息,再次发送异步通知,参数:{}",payNotifyData);

        //发送异步通知
        String rlt = restTemplate.postForObject("",payNotifyData,String.class);
        NoticeResultInfo resultInfo = resultService.queryByPaySerialNo(payNotifyData.getPasySerialNo());

        if ("success".equals(rlt)){

            if (ObjectUtils.isEmpty(resultInfo)){

                resultInfo = new NoticeResultInfo();
                resultInfo.setMchId(payNotifyData.getMchId());
                resultInfo.setNoticeCount(1); //TODO
                resultInfo.setPaySerialNo(payNotifyData.getPasySerialNo());
                resultInfo.setNoticeStatus("0"); //TODO

                Date nowDate = new Date();
                resultInfo.setCreateTime(nowDate);
                resultInfo.setSuccessTime(nowDate);

                resultService.createNoticeResult(resultInfo);
            }else {

                NoticeResultInfo record = new NoticeResultInfo();

                record.setId(resultInfo.getId());
                Date nowDate = new Date();
                record.setCreateTime(nowDate);
                record.setSuccessTime(nowDate);;
                record.setNoticeCount(resultInfo.getNoticeCount() + 1);
                resultInfo.setNoticeCount(-1); //TODO
                resultService.updateNoticeResult(record);

            }

            return;
        }

        //再次进入队列


        //延迟时间

        long time = noticeRuleService.getTime(payNotifyData.getMchId(),resultInfo.getNoticeCount());


        //设置消息的延迟属性
        rabbitTemplate.convertAndSend(MqConfig.PAY_NOTIFY_TOPIC_EXCHANGE,"pay.notify",payNotifyData);

        //手动确认
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            logger.error("IOException",e);
        }

    }
}
